//
//  JDPPPopupView.swift
//  DragonWallet
//
//  Created by volantPig on 2025/5/1.
//

import UIKit

class JDPPPopupView: UIViewController {

    lazy var containerView: UIView = {
        let container = UIView()
        container.makeCorner(8)
        container.backgroundColor = .white
        return container
    }()
    
    lazy var closeButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "pp_popup_close")?.withRenderingMode(.alwaysOriginal), for: .normal)
//        button.addTarget(self, action: #selector(cancelButtonAction), for: .touchUpInside)
        button.isHidden = true
        return button
    }()
    
    lazy var ppTitleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 18, weight: .semibold), textColor: color("#282726"), numberOfLines: 1)
        return titleLabel
    }()
    
    lazy var topPPContentLabel: UILabel = {
        let contentLabel = UILabel()
        contentLabel.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 15, weight: .regular), textColor: color("#282726"), numberOfLines: 0)
        return contentLabel
    }()
    
    lazy var bottomPPContentLabel: LinkLabel = {
        let contentLabel = LinkLabel()
        contentLabel.numberOfLines = 0
        return contentLabel
    }()
    
    lazy var bottonLine: UIView = {
        let line = UIView()
        line.backgroundColor = color("#D7DCE5")
        return line
    }()
    
    lazy var cancelButton: UIButton = {
        let button = UIButton(type: .custom)
        button.buttonConfig(normalTitle: "取消", normalImageNamed: "", normalTitleColor: color("#333333"), titleFont: UIFont.systemFont(ofSize: 16, weight: .regular), backgroundColor: color("#F5F6F8"))
        button.makeCorner(25)
//        button.addTarget(self, action: #selector(cancelButtonAction), for: .touchUpInside)
        return button
    }()
    
    lazy var confirmButton: UIButton = {
        let button = UIButton(type: .custom)
        button.buttonConfig(normalTitle: "同意并继续", normalImageNamed: "", normalTitleColor: color("#FFFFFF"), titleFont: UIFont.systemFont(ofSize: 16, weight: .regular), backgroundColor: color("#FF5C37"))
        button.makeCorner(25)
//        button.addTarget(self, action: #selector(confirmButtonAction), for: .touchUpInside)
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = color("#000000").withAlphaComponent(0.6)
        
        self.setupUIElements()
        
        self.setData()
        // Do any additional setup after loading the view.
    }
    
    func setupUIElements() {
        self.view.addSubview(self.containerView)
        self.containerView.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.centerY.equalTo(self.view)
            make.width.lessThanOrEqualTo(screenWidth-32*2)
        }
        
        self.containerView.addSubview(self.closeButton)
        self.closeButton.snp.makeConstraints { make in
            make.trailing.equalTo(self.containerView).offset(-16)
            make.top.equalTo(self.containerView).offset(16)
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
        
        self.containerView.addSubview(self.ppTitleLabel)
        self.ppTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(self.containerView).offset(32)
            make.centerX.equalTo(self.containerView)
        }
        
//        lazy var topPPContentLabel: UILabel = {
//            let contentLabel = UILabel()
//            return contentLabel
//        }()
        
        self.containerView.addSubview(self.topPPContentLabel)
        self.topPPContentLabel.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView).offset(20)
            make.trailing.equalTo(self.containerView).offset(-20)
            make.top.equalTo(self.ppTitleLabel.snp.bottom).offset(18)
        }
        
        self.containerView.addSubview(self.bottomPPContentLabel)
        self.bottomPPContentLabel.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView).offset(20)
            make.trailing.equalTo(self.containerView).offset(-20)
            make.top.equalTo(self.topPPContentLabel.snp.bottom).offset(16)
        }
        
        
        self.containerView.addSubview(self.bottonLine)
        self.bottonLine.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView).offset(20)
            make.trailing.equalTo(self.containerView).offset(-20)
            make.top.equalTo(self.bottomPPContentLabel.snp.bottom).offset(22)
            make.height.equalTo(0.5)
        }
        
        self.containerView.addSubview(self.cancelButton)
        self.cancelButton.snp.makeConstraints { make in
            make.trailing.equalTo(self.containerView.snp.centerX).offset(-5)
            make.top.equalTo(self.bottonLine.snp.bottom).offset(18)
            make.height.equalTo(50)
            make.leading.equalTo(self.containerView).offset(20)
            make.bottom.equalTo(self.containerView).offset(-18)
        }
        
        self.containerView.addSubview(self.confirmButton)
        self.confirmButton.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView.snp.centerX).offset(5)
            make.top.equalTo(self.bottonLine.snp.bottom).offset(18)
            make.height.equalTo(50)
            make.trailing.equalTo(self.containerView).offset(-20)
        }
        
    }

    func setData() {
        self.ppTitleLabel.text = "用户协议与隐私政策"
        self.topPPContentLabel.text = "(1)《隐私政策》中关于个人设备用户信息的手机和使用的说明。\n(2)《隐私政策》中与第三方SDK类服务商数据共享、相关信息收集和使用说明。\n用户协议和隐私政策说明:"
        //"感谢您信任并使用小尨包，我们依据最新的法律要求，更新了平台的《用户协议》和《隐私政策》，特意向您推送本提示，请您仔细阅读并充分理解相关条款。您点击“同意”即表示，您已阅读并同意更新后的协议和政策条款，我们将尽全力保障您的合法权益并为您提供更优质的产品和服务。"
        
        let fullStr = "阅读完整的《用户协议》和《隐私政策》了解详细的内容。"
        
        let linkModel1 = LinkLabelModel()
        linkModel1.linkString = "《用户协议》"
        linkModel1.linkStrAttributes = [NSAttributedString.Key.foregroundColor: color("#FA6161"), .font: UIFont.systemFont(ofSize: 15, weight: .regular) as Any]
        
        
        let linkModel2 = LinkLabelModel()
        linkModel2.linkString = "《隐私政策》"
        linkModel2.linkStrAttributes = [NSAttributedString.Key.foregroundColor: color("#FA6161"), .font: UIFont.systemFont(ofSize: 15, weight: .regular) as Any]
        
        self.bottomPPContentLabel.configureLinkInfo(fullStr, [.foregroundColor: color("#282726"), .font: UIFont.systemFont(ofSize: 15, weight: .regular) as Any ], [linkModel1,linkModel2])
        
        self.bottomPPContentLabel.selectLinkStringCompeleted = {  linkStr in
            
            if linkStr == "《用户协议》" {
                if let callUrl = URL(string: "https://www.xiaolongbaobao.com/yonghuxieyi.html") {
                    UIApplication.shared.open(callUrl, options: [:]) { isSuccess in
                    }
                }
            } else if linkStr == "《隐私政策》" {
                if let callUrl = URL(string: "https://www.xiaolongbaobao.com/xieyi.html") {
                    UIApplication.shared.open(callUrl, options: [:]) { isSuccess in
                    }
                }
            }
            printLog("点击的超链接:\(linkStr)")
        }
        
        self.cancelButton.addTarget(self, action: #selector(cancelButtonAction), for: .touchUpInside)
        self.confirmButton.addTarget(self, action: #selector(confirmButtonAction), for: .touchUpInside)
        
    }
    
    @objc func confirmButtonAction() {
//        self.dismiss(animated: false)
        self.view.removeFromSuperview()
    }
    
    @objc func cancelButtonAction() {
        exit(0)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
